﻿using System.Collections;
using System.Data;

//using System.Collections.Generic;
using System.IO;

namespace DMirrorMySQL
{
    /// <summary>
    /// Worker that manage list files
    /// </summary>
    public class ListWorker
    {
        private DMirror d;
        private int Site_ID;

        public ListWorker(ref DMirror dm, int site_id)
        {
            d = dm;
            Site_ID = site_id;
        }

        public void sync_ls_list(string fileName)
        {
            Hashtable ht = new Hashtable();
            ht.Clear();
            string[] lsrow;
            FileStream fs = new FileStream(fileName, FileMode.Open);
            StreamReader sr = new StreamReader(fs);
            //skip first line
            sr.ReadLine();
            string m;
            while (!sr.EndOfStream)
            {
                lsrow = sr.ReadLine().Split(' ');
                //ls -l | awk '{print $5,$9}' > ../ore_.lst
                m = Path.GetFileNameWithoutExtension(lsrow[1]);
                if (!ht.ContainsKey(m)) ht.Add(m, lsrow[0]);
            }
            d.bulkSyncPosts(Site_ID, ref ht);
            ht.Clear();
        }

        public void get_conflict_list(string fileName)
        {
            DataTable toget = new DataTable();
            d.getPost(Site_ID, ref toget, "'-2'");
            FileStream fs = new FileStream(fileName + ".sh", FileMode.Create);
            StreamWriter sw = new StreamWriter(fs);
            string url, ext;
            for (int i = 0; i < toget.Rows.Count; i++)
            {
                url = toget.Rows[i]["url"].ToString();
                ext = Path.GetExtension(url);
                url = url.Substring(0, url.LastIndexOf("/"));
                sw.WriteLine(string.Format("mv {0}{1} TOSOMEWHERE",
                    toget.Rows[i]["post_id"].ToString(),
                    ext
                    ));
            }
            sw.Flush();
            fs.Close();
        }

        public void get_broken_list(string spath, int LSTCount)
        {
            DataTable toget = new DataTable();
            Hashtable shoplists = new Hashtable();
            Hashtable shopfiles = new Hashtable();
            string url, ext;
            d.getPost(Site_ID, ref toget, "'0','-2','-1'");
            FileStream fs;// = new FileStream(listName, FileMode.Create);
            StreamWriter sw;
            for (int i = 0; i < LSTCount; i++)
            {
                fs = new FileStream(string.Format("{0}_{1}.lst",
                    spath, i.ToString()), FileMode.Create);
                shopfiles.Add(i, fs);
                shoplists.Add(i, new StreamWriter(fs));
            }

            for (int i = 0; i < toget.Rows.Count; i++)
            {
                sw = (StreamWriter)shoplists[i % LSTCount];
                url = toget.Rows[i]["url"].ToString();
                ext = Path.GetExtension(url);
                url = url.Substring(0, url.LastIndexOf("/"));
                sw.WriteLine(string.Format("{0}/{1}{2}",
                    url,
                    toget.Rows[i]["post_id"].ToString(),
                    ext
                    ));
            }
            for (int i = 0; i < LSTCount; i++)
            {
                sw = (StreamWriter)shoplists[i];
                sw.Close();
                fs = (FileStream)shopfiles[i];
                fs.Close();
            }
        }

        public void reWrite_openMeta(string fileName)
        {
        }

        public void convLineBreak(string fn, string format)
        {
            /*Windows:   \r\n
              Unix:   \n
              Mac:   \r*/
        }
    }
}